package studio.xiaoyun.core.parameter.criterion;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import studio.xiaoyun.core.exception.CoreException;
import studio.xiaoyun.core.parameter.Parameter;

/**
 * 小于等于
 * @author 岳正灵
 */
public class CriterionLe extends Criterion {
	private static final long serialVersionUID = 5880662877632272841L;
	private String propertyName;
	private Object value;

	protected CriterionLe(String propertyName, Object value) {
		this.propertyName = propertyName;
		this.value = value;
	}

	@Override
	public List<String> getPropertyName() {
		return Arrays.asList(propertyName);
	}

	@Override
	public String getQuery(Parameter parameter) {
		StringBuffer result = new StringBuffer();
		DataType dataType = this.getDataType(parameter, propertyName);
		switch (dataType) {
		case DATE:
			result.append("%s <= '");
			if (value instanceof Date) {
				result.append(this.formatDate((Date) value));
			} else {
				result.append(value.toString());
			}
			result.append("'");
			break;
		case NUMBER:
			result.append("%s <= ").append(value.toString());
			break;
		default:
			throw new CoreException("属性" + propertyName + "不支持le");
		}
		return result.toString();
	}

}
